/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fei.ClassDiagram.Editor.Wrappers;

import org.fei.ClassDiagram.XMI.XmiTypePlaceHolder;
import org.netbeans.modules.classfile.Parameter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Tomas
 */
public class MethodParameterWrapper extends BasicClassElementWrapper {

    private Parameter param;
    private String descriptor;

    public MethodParameterWrapper(Parameter param) {

        super(param);

        this.param = param;

        String type = param.getDescriptor();
        if (type.contains("L") && type.contains(";")) {
            type = type.substring(type.indexOf("L") + 1, type.indexOf(";"));
            type = type.replace("/", ".");
        }
        else {
            type = param.getDeclaration().split(" ")[0];
        }
        
        
        descriptor = type;
    }
    
    public MethodParameterWrapper(String name, String returnType) {
        super(name, returnType);
        
        descriptor = returnType + " " + name;
        param = null;
    }

    public Parameter getParameter() {
        return param;
    }
    
    public String getDescriptor() {
        return descriptor;
    }

    public Element xmiCreateParameterElement(Document document) {
    
        Element type = document.createElement("UML:Parameter.type");
        Element dataTypeElement = document.createElement("UML:DataType");
        Element parameter = document.createElement("UML:Parameter");
        
        parameter.setAttribute("xmi.id", this.xmiid);
        parameter.setAttribute("name", this.name);
        parameter.setAttribute("isSpecification", "false");
        parameter.setAttribute("kind", "in");

        dataTypeElement.setAttribute("xmi.idref", XmiTypePlaceHolder.getTypeId(this.returnType));
        
        type.appendChild(dataTypeElement);
        
        parameter.appendChild(type);
        
        return parameter;
    }
}
